package com.adou.p2p.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.adou.p2p.model.UserValue;

public class LoginInterceptor implements HandlerInterceptor  {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//      System.out.println("执行了TestInterceptor的preHandle方法");
      try {
          //统一拦截（查询当前session是否存在user）(这里user会在每次登陆成功后，写入session)
          UserValue user=(UserValue)request.getSession().getAttribute("user");
          if (user == null || user.equals(""))  {
              response.sendRedirect("/login.jsp");
              return true;
          }
          
      } catch (IOException e) {
          e.printStackTrace();
      }
      return true;//如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
                    //如果设置为true时，请求将会继续执行后面的操作
	}
}
